/*
 * @Author: your name
 * @Date: 2021-04-17 13:29:03
 * @LastEditTime: 2021-04-17 13:33:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-xmall\src\api\ajax.js
 */ 
import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'

const Ajax = new axios.create({
  baseURL: '/api',
  timeout: 5000,
}) 
//请求拦截器
Ajax.interceptors.request.use(config => {
  NProgress.start()

  let token = store.state.userInfo.token
  if(token){
      config.headers.token = token

  }
  return config
})
//响应拦截器
Ajax.interceptors.response.use((response) => {
  NProgress.done()
  // alert('请求成功')
  return response.data;
}, (error) => {
  NProgress.done()
  alert('请求失败', error.message)
  return new Promise(() => { }) //中断promise链
})
export default Ajax